package servlet;

import entity.Utente;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import session.MailUtilRemote;
import session.UtenteFacadeRemote;
import static servlet.ServletUtil.*;

/**
 * Questa servlet si occupa di gestire gli amici degli utenti.
 *
 * @author Rachid Ouchary, Fabio Strocco, Max Passaghe, Marco Tansini.
 */
public class GestoreAmici extends HttpServlet {
    @EJB
    private MailUtilRemote mailUtil;
    @EJB
    private UtenteFacadeRemote utenteFacade;


    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String operation = getOperation(request);

        if (operation.equals(ACTION_AGGIUNGI_AMICO)) {
            aggiungiAmico(request, response,
                          request.getParameter(PARAM_USERNAME_AMICO));
        } else if (operation.equals(ACTION_CERCA_AMICI)) {
            String nome = request.getParameter(PARAM_NOME);
            cercaAmici(request, response, request.getParameter(PARAM_RIC_USERNAME),
                                          request.getParameter(PARAM_RIC_NOME),
                                          request.getParameter(PARAM_RIC_COGNOME),
                                          request.getParameter(PARAM_RIC_EMAIL),
                                          request.getParameter(PARAM_CERCA_AMICI) != null);
        }

    }

    // <editor-fold defaultstate="collapsed" desc="Eventi gestiti dal controller">
    /*
     * Aggiunge un amico all'utente.
     */
    private void aggiungiAmico(HttpServletRequest request,
                              HttpServletResponse response,
                              String username) throws IOException, ServletException {
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        String current = (String) session.getAttribute(SESSION_UTENTE);
        Utente mittente = utenteFacade.find(current);
        Utente destinatario = utenteFacade.find(username);
        utenteFacade.aggiungiAmico(current, username);
        mailUtil.sendFriendMail(mittente, destinatario);
        request.setAttribute(PARAM_MESSAGE, username + " &egrave stato " +
                                                       "aggiunto con successo. Il tuo nuovo amico " +
                                                       "ricever&agrave una notifica per email");
        request.getRequestDispatcher("Message.jsp").forward(request, response);
    }

    /*
     * Cerca un amico dell'utente.
     */
    private void cercaAmici(HttpServletRequest request,
                            HttpServletResponse response,
                            String username,
                            String nome,
                            String cognome,
                            String email,
                            boolean soloAmici) throws IOException, ServletException {

        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        String current = (String) session.getAttribute(SESSION_UTENTE);
        Utente utente = (current != null ? utenteFacade.find(current) : null);
        List<Utente> amici = utenteFacade.cercaAmici(current, username, nome, cognome, email, soloAmici);
        request.setAttribute(PARAM_UTENTE, utente);
        request.setAttribute(PARAM_LISTA_RICERCA, amici);
        request.getRequestDispatcher("CercaAmici.jsp").forward(request, response);
    }
    // </editor-fold>



    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
